package com.wu.family.feed;



import com.wu.family.R;
import com.wu.family.config.Constant;
import com.wu.family.model.Audio;
import com.wu.family.utils.audio.SoundMgr;
import com.wu.family.utils.audio.SoundPlayer;
import com.wu.family.utils.audio.SoundMgr.SoundCallBack;
import com.wu.family.utils.audio.SoundPlayer.OnCompleteItf;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AudioCommentView extends FrameLayout {
	private static final String TAG = AudioCommentView.class.getSimpleName();

	private LinearLayout llAudio;
	private TextView tvAudioTime;
	private ImageView ivAudioIcon;
	private Context mContext;
	
	private Audio audio = new Audio();
	
	public AudioCommentView(Context context) {
		super(context);
		mContext = context;
		init();
	}
	public AudioCommentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
		
	}
	private void init() {
		if (isInEditMode()) {
            return;
        }
		LayoutInflater.from(mContext).inflate(R.layout.audio_comment, this, true);
		llAudio = (LinearLayout) findViewById( R.id.llAudio );
		tvAudioTime = (TextView) findViewById( R.id.tvAudioTime );
		ivAudioIcon = (ImageView) findViewById( R.id.ivAudioIcon );
		
		setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (audio.isPlaying()) {
					SoundPlayer.getInstance().stop();
					audio.setPlaying(false);
					ivAudioIcon.setImageResource(R.drawable.audio_btn_play);
				}else {
					SoundMgr.getInstance().getSoundPath(audio.getAudioUrl(), new SoundCallBack(){
						@Override
						public void onFoundCachePath(String url, String path) {
							SoundPlayer.getInstance().playMusic(path, new OnCompleteItf() {
								@Override
								public void OnPreStart(int duration) {
									audio.setPlaying(true);
									ivAudioIcon.setImageResource(R.drawable.audio_btn_stop);
									/*Intent intent = new Intent(Constant.Action.START_SOUND_BROADCAST);
									intent.putExtra("AudioPhoto", audio);
									intent.putExtra("position", position); 
									context.sendBroadcast(intent);*/
								}
								@Override
								public void OnComplete() {
									ivAudioIcon.setImageResource(R.drawable.audio_btn_play);
									audio.setPlaying(false);
								}

								@Override
								public void OnUnComplete() {
									ivAudioIcon.setImageResource(R.drawable.audio_btn_play);
									audio.setPlaying(false);
								}
							});
						}});
				}
			}
		});
	}
	public Audio getAudio() {
		return audio;
	}
	public void setAudio(Audio audio) {
		this.audio = audio;
		if (audio != null) {
			tvAudioTime.setText(String.format(getResources().getString(R.string.audio_time_format), audio.getAudioTime()));
			if (audio.isPlaying()) {
				ivAudioIcon.setImageResource(R.drawable.audio_btn_stop);
			}else {
				ivAudioIcon.setImageResource(R.drawable.audio_btn_play);
			}
		}
	}
	
}
